import config from "@/config";
import storage from "@/utils/storage";
import constant from "@/utils/constant";
import { login, logout, getInfo, newLogin } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import {
  getOrganizationId,
  setOrganizationId,
  removeOrganizationId,
} from "@/utils/auth2";
import {
  getframe,
  setframe,
  removeframe,
  gettime0,
  settime0,
  removetime0,
  gettime1,
  settime1,
  removetime1,
  gettime2,
  settime2,
  removetime2,
} from "@/utils/auth3";
import { geturl, seturl, removeurl } from "@/utils/auth4";
import { getinfo, setinfo, removeinfo } from "@/utils/auth5";
import { getsubject, setsubject, removesubject } from "@/utils/auth6";
import { getperiod, setperiod, removeperiod } from "@/utils/auth7";
import { getgrade, setgrade, removegrade } from "@/utils/auth8";
import { handleImageUrl } from "../../utils/common";

const baseUrl = config.baseUrl;

const user = {
  state: {
	  uiStyle:'normal',
    token: getToken(),
    name: storage.get(constant.name),
    avatar: storage.get(constant.avatar),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions),
    url: "/pages/index",
    urlstartask: "/pages/index",
    urlquestion: "/pages/hot/index",
    urlstudio: "/pages/index",
    urllogin: "/pages/index",
    askconsult: true,
    questionid: 0,
    questioni: 0,
    studio: {
      id: 0,
      studioName: "",
      introduction: "",
    },
    personal: {
      avatar: "",
      nickName: "",
      consultsCount: 0,
      evaluatesCount: 0,
      questionsCount: 0,
    },
    myquestion: null,
    checked: true,
    evaluate: {
      kind: 0,
      questionOrCounsultId: 0,
      score: 5,
      teacherId: 0,
    },
    baseUrl: config.baseUrl,
    refresh: true,
    refresh2: true,
    switch: 0,
    gradeId: 0,
    subjectId: 0,
    periodId: 0,
    periodlist: [
      { value: 0, text: "小学" },
      { value: 1, text: "初中" },
    ],
    subjectlist: [
      [
        { value: 0, text: "语文" },
        { value: 1, text: "数学" },
        { value: 2, text: "英语" },
      ],
      [
        { value: 0, text: "语文" },
        { value: 1, text: "数学" },
        { value: 2, text: "英语" },
        { value: 3, text: "科学" },
      ],
    ],
    organizationId: getOrganizationId(),
    recommend: {
      question: "",
      periodId: 0,
      subjectId: 0,
      gradeId: 0,
    },
    prompt: {
      consultsCount: 0,
      evaluatesCount: 0,
      questionsCount: 0,
      first: true,
    },
    time0: gettime0(),
    time1: gettime1(),
    time2: gettime2(),
    timeurl: geturl(),
    voicelist: [],
    info: getinfo(),
    promptflag: true,
    teacherName: null,
    videodetails: null,
    isApp: null,
    ticket: null,
    test: false,
    isBelongCity: 1,
  },

  mutations: {
	SET_UISTYLE(state, data) {
		  state.uiStyle = data;
		  window.document.documentElement.setAttribute(
			"data-size",
			data == "elder" ? "1" : "0"
		  );
		storage.set("uiStyle", state.uiStyle);
	},  
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
      storage.set(constant.name, name);
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
      storage.set(constant.avatar, avatar);
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
      storage.set(constant.roles, roles);
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
      storage.set(constant.permissions, permissions);
    },
    SET_BASEURL: (state, baseUrl) => {
      state.baseUrl = baseUrl;
    },

    SET_PERIODID: (state, periodId) => {
      state.periodId = periodId;
      storage.set(constant.periodId, periodId);
    },
    SET_SUBJECTID: (state, subjectId) => {
      state.subjectId = subjectId;
      storage.set(constant.subjectId, subjectId);
    },
    SET_GRADEID: (state, gradeId) => {
      state.gradeId = gradeId;
      storage.set(constant.gradeId, gradeId);
    },

    SET_SUBJECTLIST: (state, subjectlist) => {
      state.subjectlist = subjectlist;
      storage.set(constant.subjectlist, subjectlist);
    },
    SET_PERIODLIST: (state, periodlist) => {
      state.periodlist = periodlist;
      storage.set(constant.periodlist, periodlist);
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      const isApp = userInfo.isApp;
      return new Promise((resolve, reject) => {
        newLogin(username, password, code, uuid, isApp)
          .then((res) => {
            setToken(res.token);
            commit("SET_TOKEN", res.token);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      commit("SET_BASEURL", baseUrl);
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res) => {
            const user = res.user;
            const avatar =
              user == null || user.avatar == "" || user.avatar == null
                ? require("@/static/images/profile.jpg")
                : handleImageUrl(user.avatar);
            const username =
              user == null || user.userName == "" || user.userName == null
                ? ""
                : user.userName;
            const periodId =
              user == null || user.periodId == "" || user.periodId == null
                ? ""
                : user.periodId;
            const subjectId =
              user == null || user.subjectId == "" || user.subjectId == null
                ? ""
                : user.subjectId;
            const gradeId =
              user == null || user.gradeId == "" || user.gradeId == null
                ? ""
                : user.gradeId;
            if (res.roles && res.roles.length > 0) {
              commit("SET_ROLES", res.roles);
              commit("SET_PERMISSIONS", res.permissions);
            } else {
              commit("SET_ROLES", ["ROLE_DEFAULT"]);
            }
            commit("SET_NAME", username);
            commit("SET_AVATAR", avatar);
            commit("SET_PERIOD", periodId);
            commit("SET_SUBJECT", subjectId);
            commit("SET_GRADEID", gradeId);
            setinfo(user);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_PERMISSIONS", []);
            removeinfo();
            removeToken();
            storage.clean();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
};

export default user;
